<template>
    <view class="page-creation-book">
        <u-form ref="form1" label-width="160rpx">
                <u-form-item label="正文" label-position="top">
                    <u-input v-model="form.content" type="textarea" auto-height placeholder="写下你读书过程中，或者线下读书会讨论中，你所感受到的思想或是启发..."/>
                </u-form-item>
                <u-form-item label="上传图片" label-position="top">
                    <u-upload :action="upload.api" :header="upload.header" :file-list="upload.coverImageUrl" :max-count="1" :multiple="false" @on-success="coverSuccess" @on-error="coverError" :auto-upload="true"></u-upload>
                </u-form-item>
                <u-form-item label="关闭评论区">
                    <template v-slot:right>
                        <u-switch v-model="form.isCheckinRequired" size="40"></u-switch>
                    </template>
                </u-form-item>
                <u-form-item label="仅自己可见">
                    <template v-slot:right>
                        <u-switch v-model="form.isPrivate" size="40"></u-switch>
                    </template>
                </u-form-item>
                <u-form-item label="不展示再首页" label-width="200rpx">
                    <template v-slot:right>
                        <u-switch v-model="form.isFeatured" size="40"></u-switch>
                    </template>
                </u-form-item>
                <u-form-item label="内容涉及心理创伤，请谨慎评论" label-width="400rpx">
                    <template v-slot:right>
                        <u-switch v-model="form.sensitiveContentWarning" size="40"></u-switch>
                    </template>
                </u-form-item>
            </u-form>
        <div class="mt-4">
            <u-row gutter="16">
                <u-col span="6">
                    <u-button class="btn-grey" @click="submitForm(0)">保存</u-button>
                </u-col>
                <u-col span="6">
                    <u-button class="btn-cyan" @click="submitForm(1)">发布</u-button>
                </u-col>
            </u-row>
        </div>
    </view>
</template>
<script setup lang="ts">
import {apiUrl} from '@/static/js/config';
import {onShow, onLoad } from '@dcloudio/uni-app';
import { useAppStore } from '@/stores/app';
const appStore = useAppStore();
const {proxy} = getCurrentInstance() as ComponentInternalInstance;
let pageParams = reactive({});
let form = reactive({
  "title": "",
  "content": "",
  "userId": null,
  "coverImageUrl": "",
  "bookName": "",
  "bookAuthor": "",
  "articlesType": "",
  "tags": "",
  "recommendationReason": "",
  "isCommentDisabled": false,
  "isPrivate": false,
  "isFeatured": false,
  "sensitiveContentWarning": false,
  "activitiesId": 0,
  "status": 1,
});
let upload = reactive({
    // api: 'http://book.caochuanxi.com/api/admin/sys-file/upload',
    api: apiUrl + '/admin/sys-file/upload',
    header: {
        Authorization: `Bearer ${uni.getStorageSync('accessToken')}`,
        "TENANT-ID": 1,
        'CLIENT-TOC': 'Y'
    },
    coverImageUrl: [],
});
const coverSuccess = (res) => {
    form.coverImageUrl = res.data.url;
}
const coverError = (res) => {

};
const getDetail = () => {
    proxy.$http('get', '/admin/articles/'+ pageParams.id, {}, (data,msg) => {
        proxy.$util.resetForm(form, data);
        form.isCommentDisabled = form.isCommentDisabled == '1' ? true : false;
        form.isPrivate = form.isPrivate == '1' ? true : false;
        form.isFeatured = form.isFeatured == '1' ? true : false;
        form.sensitiveContentWarning = form.sensitiveContentWarning == '1' ? true : false;
    })
}
const submitForm = (status) => {
    let params = JSON.parse(JSON.stringify(form));
    params.isCommentDisabled = form.isCommentDisabled ? '1' : '0';
    params.isPrivate = form.isPrivate ? '1' : '0';
    params.isFeatured = form.isFeatured ? '1' : '0';
    params.sensitiveContentWarning = form.sensitiveContentWarning ? '1' : '0';
    params.status = status;
    uni.showLoading({mask: true});
    proxy.$http('post', '/admin/articles', params, (data,msg) => {
        uni.hideLoading();
        uni.$u.toast('提交成功');
    },msg => {
        uni.hideLoading();
    })
};
onLoad((e) => {
    pageParams = e;
});
onShow(() => {    
    if(pageParams.id){
        getDetail();
    }else{
        let userInfo = JSON.parse(uni.getStorageSync('userInfo'))
        form.userId = userInfo.userId;
        form.activitiesId = pageParams.activitiesId;
    }
});
</script>
<style>
page{height:100%;background:#fff;}
</style>